﻿using UnityEngine;
using System.Collections;

public class Camera_Move : MonoBehaviour
{
    public float xMargin = 1f;
    public float yMargin = 2.78f;
    public float xSmooth = 0.5f;
    public float ySmooth = 0.5f;
    //public Vector2 maxXAndY;
    //public Vector2 minXAndY;
    public GameObject _Player;
    private Transform player;
    private float targetX;
    private float targetY;

    void Start()
    {
        player = _Player.GetComponent<Transform>();
    }

    bool CheckXMargin()
    {
        return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
    }

    bool CheckYMargin()
    {
        return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
    }

    void FixedUpdate()
    {
        TrackPlayer();

    }

    void TrackPlayer()
    {
        targetX = transform.position.x;
        targetY = transform.position.y;
        if (CheckXMargin())
            targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.deltaTime);

        if (CheckYMargin())
            targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.deltaTime);

        //targetX = Mathf.Clamp (targetX, minXAndY.x, maxXAndY.x);
        //targetY = Mathf.Clamp (targetY, minXAndY.y, maxXAndY.y);

        transform.position = new Vector3(targetX, targetY, transform.position.z);
    }
}
